import AdItem from "./Items/AdItem";
import { ui } from "../../../../ui/layaMaxUI";
import { AdConfigData, MoveDir } from "./ShiyouAdManager";
import { BaseAdView } from "./BaseAdView";
import TimeTool from "../../Tool/TimeTool";


export default class ThreeSmallAdView extends ui.ShiyouAdView.ThreeAdViewUI implements BaseAdView 
{
    
    private m_moveDir:MoveDir=MoveDir.None;

    private m_moveSpeed:number=37;
    private m_moveMaxValue:number=1;

    InitData(addata: Array<AdConfigData>) 
    {
        if (addata.length < 1) {
            return;
        }
        let data = [];
        for (let index = 0; index < addata.length; index++) {
            const element = addata[index];
            if (element.Type10 !=null) {
                data.push(element);
            }
        }
        if (data.length < 1) {
            return;
        }
        data.sort(this.Sort.bind(this));
        this.m_AdList.array = data;
        this.m_AdList.repeatX = data.length;
        this.m_moveMaxValue= this.m_AdList.scrollBar.max;
        this.m_moveDir=MoveDir.Forward;
    }

    // OnAdListClick() {
    //     this.ListTweenToEnd();
    // }

    constructor()  {
        super();
        Laya.stage.addChild(this);
        this.m_AdList.on(Laya.Event.MOUSE_DOWN, this, this.OnMouseDown);
        this.m_AdList.on(Laya.Event.MOUSE_OUT, this, this.OnMouseUp);
        this.m_AdList.on(Laya.Event.MOUSE_UP, this, this.OnMouseUp);
    }

    createChildren()  {
        super.createChildren();
        this.m_AdList.renderHandler = new Laya.Handler(this, this.OnRenderHandler);
     
         this.m_AdList.elasticEnabled = true;
         this.m_AdList.selectEnable = true;
        this.m_AdList.hScrollBarSkin = "";
    }


    public OnRenderHandler(cell: AdItem, index: number)  {
        let skindata = cell.dataSource;
        cell.SetData(skindata,"Type10",skindata.Type10);
    }


    update()
    {
        if(this.m_AdList.length<=3){
            return;
        }
        switch (this.m_moveDir) {
            case MoveDir.Forward:
                this.MoveForward();
                break;
            case MoveDir.Back:
                this.MoveBack();
                break;
            default:
                break;
        }
    }

    OnMouseDown(){
        this.m_moveDir=MoveDir.None;
    }


    OnMouseUp(){
        let scrobarvalue=this.m_AdList.scrollBar.value;
        if (scrobarvalue>this.m_moveMaxValue/2) {
            this.m_moveDir=MoveDir.Forward;
        }
        else{
            this.m_moveDir=MoveDir.Back;
        }
    }

    private MoveForward(){

        if (this.m_AdList.scrollBar.value< this.m_AdList.scrollBar.max) {
            this.m_AdList.scrollBar.value+=this.m_moveSpeed*TimeTool.DeltaTime;
        }
        else{
            this.m_moveDir=MoveDir.Back;
        }
    }
   
 
    private MoveBack()
    {
        if (this.m_AdList.scrollBar.value> this.m_AdList.scrollBar.min) {
            this.m_AdList.scrollBar.value-=this.m_moveSpeed*TimeTool.DeltaTime;
        }
        else{
            this.m_moveDir=MoveDir.Forward;
        }
    }


    /**以底部为标准 小于0为顶部  大于0为底部*/
    public Show(pos:number)  
    {
        this.visible = true;
        if (pos<0) {
            this.bottom=NaN;
            this.top=Math.abs(pos);
        }
        else{
            this.top=NaN;
            this.bottom=pos;
        }
        Laya.timer.frameLoop(2,this, this.update);
    }


    public Hide(){
        Laya.timer.clearAll(this);
        this.visible = false;
    }


    Sort(a,b){
        if (a.Type10 < b.Type10)return -1;
        if (a.Type10 == b.Type10)return 0;   
        return 1;
    }
}